
package cn.xabad.commons.io;

import java.io.IOException;
import java.io.Serializable;


public class TaggedIOException extends IOExceptionWithCause {

    
    private static final long serialVersionUID = -6994123481142850163L;

    
    public static boolean isTaggedWith(Throwable throwable, Object tag) {
        return tag != null
            && throwable instanceof TaggedIOException
            && tag.equals(((TaggedIOException) throwable).tag);
    }

    
    public static void throwCauseIfTaggedWith(Throwable throwable, Object tag)
            throws IOException {
        if (isTaggedWith(throwable, tag)) {
            throw ((TaggedIOException) throwable).getCause();
        }
    }

    
    private final Serializable tag;

    
    public TaggedIOException(IOException original, Serializable tag) {
        super(original.getMessage(), original);
        this.tag = tag;
    }

    
    public Serializable getTag() {
        return tag;
    }

    
    @Override
    public IOException getCause() {
        return (IOException) super.getCause();
    }

}
